package com.tansun.rule.exception;
import java.io.Externalizable;
import java.io.IOException;
import java.io.ObjectInput;
import java.io.ObjectOutput;

import org.kie.api.runtime.rule.ConsequenceExceptionHandler;
import org.kie.api.runtime.rule.Match;
import org.kie.api.runtime.rule.RuleRuntime;

import com.tansun.rule.utils.DroolsRuleLog;

/**
 * drools异常类处理
 * @author xuconghui
 * @date 2020年4月30日
 */
public class RuleConsequenceExceptionHandler  implements ConsequenceExceptionHandler, Externalizable {

	public static DroolsRuleLog  droolsRuleLog;
    public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException {
    }

    public void writeExternal(ObjectOutput out) throws IOException {
    }

    public void handleException(Match activation,
                                RuleRuntime workingMemory,
                                Exception exception) {
    	if(droolsRuleLog!=null) {
    		droolsRuleLog.addExceptionLogs(null,activation.getRule().getName(), activation.getDeclarationValue("$insertMap"), exception.getMessage());
    	}else{
    		System.out.println("未设置RuleConsequenceExceptionHandler的属性droolsRuleLog");
    	}
        throw new org.kie.api.runtime.rule.ConsequenceException(exception, workingMemory, activation );
    }

}
